<?
//Voting 0.0.1
class sym_voting extends symbiont{
    public function ___main($design='', $name='', $id=''){
        global $sql;
        if(data::is_uint($id)){
            $voting=$sql->query_each("
                            SELECT v.id, l.value as question
                            FROM `sym_voting` as v
                                LEFT JOIN `labels` as l ON v.question_lid = l.id
                            WHERE v.id = '".$id."'", 1);
        }
        else{
            $name=data::word($name);
            if($name==='') return;
            $voting=$sql->query_each("
                            SELECT v.id, l.value as question
                            FROM `sym_voting` as v
                                LEFT JOIN `labels` as l ON v.question_lid = l.id
                            WHERE v.name = '".$name."'", 1);
        }
        if(!is_array($voting)) return;
        $variants=$sql->query_each("
                            SELECT v.id, l.value as variant, v.answers
                            FROM `sym_voting_variants` as v
                                LEFT JOIN `labels` as l ON v.variant_lid = l.id
                            WHERE v.voting_id = ".$voting['id']."
                            ORDER BY position
                            ");
        if(!is_array($variants)) return;
        $val='sym_voting_'.$voting['id'];
        if(isset($_COOKIE[$val])){
            $design='results';
        }
        $design=$this->_check($design);
        
        $d=new design($design, 'voting', false);
        
        $this->voting_id=$voting['id'];
        $this->name=$name;
        $this->question=$voting['question'];
        
        $this->variants='';
        $i=0;
        foreach($variants as $variant){
            $this->i=$i++;
            $this->answers=$variant['answers'];
            $this->id=$variant['id'];
            $this->title=$variant['variant'];
            $this->variants.=design::run($this->variant);
        }
        $d->inc();
    }
    public function ___vote($design=''){
        if(!isset($_POST['voting_id'])||!isset($_POST['variant_id'])){
            $this->___main($design,'', $voting_id);
            return;
        }
        $variant_id=$_POST['variant_id'];
        $voting_id=$_POST['voting_id'];
        if(!data::is_uint($variant_id)&&data::is_uint($voting_id)){
            $this->___main($design,'', $voting_id);
            return;
        }
        $val='sym_voting_'.$voting_id;
        if(isset($_COOKIE[$val])){
            $this->___main($design,'', $voting_id);
            return;
        }
        global $sql, $kernel;
        $sql->query("UPDATE `sym_voting_variants` SET answers=answers+1 WHERE id=".$variant_id." AND voting_id =".$voting_id."");
        setcookie($val, '1', 0, '/');
        $this->___main($design,'', $voting_id);
    }
    public function ___admin($design=''){
        global $kernel, $sym;
        $kernel->symbiont_add('voting-admin');
        $sym->voting_admin->main($design);
    }
    public function _administrate($symbiont){
        global $sql;
        $design=$this->_check('administrate');
        $d=new design($design, 'voting', false);
        $this->options='';
        $votings=$sql->query_each("
                            SELECT v.id, v.name, l.value as question
                            FROM `sym_voting` as v
                                LEFT JOIN `labels` as l ON v.question_lid = l.id
                            ORDER BY question");
        foreach($votings as $voting){
            $this->id=$voting['id'];
            $this->name=$voting['name'];
            $this->question=$voting['question']?$voting['question']:$voting['name'];
            $this->options.=design::run($this->option);
        }
        $d->inc();
    }
}
?>